use elasticsearch::{ Search, SearchParts};
use elasticsearch::http::request::JsonBody;
use serde_json::Value;
use crate::api::client::client;
use crate::api::get_values;

pub async fn search(index: String, query: Option<String>, from: i64, size: i64) -> Option<Value> {
    match client().await {
        Ok(client) => {
            let result = Search::<JsonBody<Value>>::new(client.transport(), SearchParts::Index(&[&index]))
                .from(from)
                .size(size)
                .send()
                .await;
            get_values(result).await
        }
        Err(err) => {
            println!("{:?}", err);
            None
        }
    }
}